package bigbrother.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import bigbrother.entity.Employee;
import bigbrother.exception.ServiceFailException;

public class EmployeeDao extends Dao implements IEmployeeDao {

	Logger logger = null;
	private static EmployeeDao dao;
	
	private EmployeeDao() {
		setLogger();
	}
	public static EmployeeDao getDao(){
		if(dao==null){
			dao = new EmployeeDao();
		}
		return dao;
	}
	
	void setLogger() {
		logger = LogManager.getLogger(EmployeeDao.class);
	}

	@Override
	public int addEmployee(Connection con, Employee emp) {
		String query = "insert into employee values(?, ?, ?)";
		logger.trace(query);
		PreparedStatement pstmt = null;

		int result = 0;
		try {
			pstmt = con.prepareStatement(query);
			pstmt.setString(1, emp.getId());
			pstmt.setString(2, emp.getName());
			pstmt.setString(3, emp.getPass());
			result = pstmt.executeUpdate();
		} catch (SQLException e) {
			logger.error(e);
			if(e.getErrorCode()==1){
				throw new ServiceFailException(
						"중복된 키를 사용하고 있습니다."+emp.getId());
			}else{
				throw new ServiceFailException(e);
			}
		} finally {
			close(pstmt);
		}

		return result;
	}

	@Override
	public int updateEmployee(Connection con, Employee emp) {
		String query = "update employee set name=?,pass=? where id=?";
		logger.trace(query);
		PreparedStatement pstmt = null;

		int result = 0;

		try {
			pstmt = con.prepareStatement(query);
			pstmt.setString(1, emp.getName());
			pstmt.setString(2, emp.getPass());
			pstmt.setString(3, emp.getId());
			result = pstmt.executeUpdate();
		} catch (SQLException e) {
			logger.error(e);
			throw new ServiceFailException(e);
		} finally {
			close( pstmt);
		}
		return result;
	}

	@Override
	public int deleteEmployee(Connection con, String id) {
		String query = "delete from employee where id=?";
		logger.trace(query);
		PreparedStatement pstmt = null;
		int result = 0;
		try {
			pstmt = con.prepareStatement(query);
			pstmt.setString(1, id);
			result = pstmt.executeUpdate();
		} catch (SQLException e) {
			logger.error(e);
			throw new ServiceFailException(e);
		} finally {
			close( pstmt);
		}
		return result;
	}

	@Override
	public Employee getEmployee(Connection con, String id) {
		String query = "select * from employee where id=?";
		logger.trace(query +"  "+id);
		PreparedStatement pstmt = null;
		Employee result = null;
		ResultSet rset = null;
		try {
			pstmt = con.prepareStatement(query);
			pstmt.setString(1, id);
			rset = pstmt.executeQuery();
			if(rset.next()){
				String name = rset.getString("name");
				String pass = rset.getString("pass");
				result = new Employee(id, name, pass);
				logger.trace(result);
			}
		} catch (SQLException e) {
			logger.error(e);
			throw new ServiceFailException(e);
		} finally {
			close( pstmt, rset);
		}
		return result;
	}

	@Override
	public ArrayList<Employee> getEmployeeList(Connection con) {
		String query = "select * from employee";
		logger.trace(query);
		PreparedStatement pstmt = null;
		ArrayList<Employee> result = new ArrayList<Employee>();
		ResultSet rset = null;
		try {
			pstmt = con.prepareStatement(query);
			rset = pstmt.executeQuery();
			while(rset.next()){
				String id = rset.getString("id");
				String name = rset.getString("name");
				String pass = rset.getString("pass");
				Employee emp= new Employee(id, name, pass);
				result.add(emp);
			}
		} catch (SQLException e) {
			logger.error(e);
			throw new ServiceFailException(e);
		} finally {
			close(pstmt, rset);
		}
		return result;
	}
}
